package web.controller.user;

import common.consts.ContentType;
import common.validation.ValidationResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import service.ApplyService;
import service.ServiceResponse;
import web.Routes;
import web.controller.Base;
import web.req.ApplyRequest;
import web.security.Protection;

/**
 * Created by aceyin on 15/6/28.
 * 应聘
 */
@RestController
@RequestMapping(Routes.User.APPLY)
public class Apply extends Base {
    private static final Logger LOGGER = LoggerFactory.getLogger(Apply.class);

    @Autowired
    private ApplyService applyService;

    @Protection
    @RequestMapping(method = RequestMethod.POST, produces = ContentType.JSON)
    public ServiceResponse save(@RequestBody ApplyRequest request) {
        ValidationResult validate = request.validate();
        if (validate.isFailed()) {
            return fail(validate.getMessage());
        }
        long user = getUserIdFromAuthHeader();
        ServiceResponse response = applyService.apply(user, request.getRecruitment());
        return response;
    }
}
